import md5 from "md5";
import router from "./../router/index";
import { Dialog, Toast } from "vant";
import store from "@/store/index";

export function md5Str(string) {
  return md5(string);
}

export function isPhoneNum(number) {
  return /^[1][3,4,5,7,8,9][0-9]{9}$/.test(number);
}

export function ispwd(pwd) {
  return /^[0-9A-Za-z]{8,20}$/.test(pwd);
}
/**
 * 判断字符串是否是金钱
 */
export function validMoney(money) {
  var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (reg.test(money)) {
    return true;
  } else {
    return false;
  }
}

export function actionWithLink(link) {

  if (!link || link === ' '){
    return;
  }

  if (link.startsWith("tel://")) {
    var pattern = "tel://";
    var str = link.replace(new RegExp(pattern), "");

    Dialog.confirm({
      title: "是否拨打",
      message: str
    })
      .then(() => {
        window.location.href = link;
      })
      .catch(() => {
        // on cancel
      });
  } else if (link.startsWith("http://") || link.startsWith("https://")) {
    window.location.href = link;
  } else {
    router.push({ path: link });
  }
}

export function addImagePrefix(imageUrl) {
  // return "http://47.103.219.130:8080" + imageUrl;
  // return  "https://ktwy.pmssaas.com" + imageUrl;

  return process.env.VUE_APP_IMGURL + imageUrl;
}
export function removeImagePrefix(imageUrl) {
  // return imageUrl.replace("http://47.103.219.130:8080", "");
  // return imageUrl.replace( "https://ktwy.pmssaas.com", "");
  return imageUrl.replace(process.env.VUE_APP_IMGURL, "");
}
export function getOrderStatusStr(isMaster, status) {
  if (false) {
    // 维修师傅 1:处理中订单, 2:待付款订单, 3:已完成订单
    if (status === 1) {
      return "处理中";
    } else if (status === 2) {
      return "待付款";
    } else if (status === 3) {
      return "已完成";
    } else {
      return "其他";
    }
  } else {
    //  用户 * 1:待确认 2:处理中 3:待支付 4:待评价 5:已取消 6:已完成
    if (status === 1) {
      return "待分配";
    } else if (status === 2) {
      return "处理中";
    } else if (status === 3) {
      return "待支付";
    } else if (status === 4) {
      return "待评价";
    } else if (status === 5) {
      return "已取消";
    } else if (status === 6) {
      return "已完成";
    } else {
      return "用户其他";
    }
  }
}

// 检查微信是否授权
export function wxAuthorize(url) {
  // 1.判断本地是否有授权  - > 是否已有想要获取的信息
  if (store.state.wxInfo.openid) {
    console.log("已经授权");
    //已授权不做操作
  } else {
    //未授权 跳转授权网页
    console.log("未授权");
    // var urlStr = "https%3a%2f%2f47.103.219.130%2f8081";

    // var urlStr = "https://47.103.219.130/8081";

    var urlStr = "http%3a%2f%2f47.103.219.130%3a8081%2f%23%2fdashboard%2flogin";
    // var urlStr = "http%3a%2f%2f47.103.219.130%3a8081%2fdashboard%2flogin";

    // var urlStr = "http://47.103.219.130:8081/dashboard/login";

    // 测试公众号
    var wxappid = "wx906c9c096c7ae1e3";
    // 机场公众号
    // var wxappid = "wx9f80ace9faf60223";

    var scope = "snsapi_base";

    // var wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd7098baffd77bb1e&redirect_uri=http%3a%2f%2flocalhost%3a8081%2f%23%2fdashboard%2fhome&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    // var wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx14707f689e84c488&redirect_uri=http%3a%2f%2f47.103.219.130%3a8081&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    // var wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx906c9c096c7ae1e3&redirect_uri=https%3a%2f%2f47.103.219.130%2f8081/&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    // var wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx14707f689e84c488&redirect_uri=http://www.baidu.com&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

    var wxurl =
      "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
      wxappid +
      "&redirect_uri=" +
      urlStr +
      "&response_type=code&scope=" +
      scope +
      "&state=STATE#wechat_redirect";

    window.location.href = wxurl;
  }
}

// 登录拦截
export function loginWxAuthorize() {

    // ip回调
    // var urlStr = "http%3a%2f%2f47.103.219.130%3a8081%2f%23%2fdashboard%2flogin";
    // 域名回调
    var urlStr = "https%3a%2f%2fktwy.pmssaas.com%2f%23%2flogin";
    // 测试公众号
  // var wxappid = "wx906c9c096c7ae1e3";
  // 机场公众号
  var wxappid = "wx9f80ace9faf60223";
  var scope = "snsapi_base";

  var wxurl =
    "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
    wxappid +
    "&redirect_uri=" +
    urlStr +
    "&response_type=code&scope=" +
    scope +
    "&state=STATE#wechat_redirect";

  window.location.href = wxurl;
}

export function isWeixinBrowser() {
  var agent = navigator.userAgent.toLowerCase();
  if (agent.match(/MicroMessenger/i) == "micromessenger") {
      console.log("wx浏览器");
      return true;
  } else {
      console.log("不是wx浏览器");

      return false;
  }
}

export function checkUserOnlyPage() {

}
